import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';

class UrlLauncherPage extends StatefulWidget {
  UrlLauncherPage({Key key}) : super(key: key);

  @override
  _UrlLauncherPageState createState() => _UrlLauncherPageState();
}

class _UrlLauncherPageState extends State<UrlLauncherPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('UrlLauncher') 
      ), 
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            SizedBox(height:20),
            RaisedButton(
              child: Text('打开外部浏览器'),
              onPressed: () async{
                const url = 'https://flutter-io.cn/';
                if (await canLaunch(url)) {
                  await launch(url);
                } else {
                  throw 'Could not launch $url';
                }
              }
            ),
            SizedBox(height:20),
            RaisedButton(
              child: Text('拨打电话'),
              onPressed: () async{
                var tel = 'tel:10086';
                if (await canLaunch(tel)) {
                  await launch(tel);
                } else {
                  throw 'Could not launch $tel';
                }
              }
            ),
            SizedBox(height:20),
            RaisedButton(
              child: Text('发送短信'),
              onPressed: () async{
                var tel = 'sms:10086';
                if (await canLaunch(tel)) {
                  await launch(tel);
                } else {
                  throw 'Could not launch $tel';
                }
              }
            ),
            SizedBox(height:20),
            RaisedButton(
              child: Text('打开外部应用-微信'),
              onPressed: () async{
                /**
                 * weixin://
                 * alipays://
                 */
                var url = 'weixin://';
                if (await canLaunch(url)) {
                  await launch(url);
                } else {
                  throw 'Could not launch $url';
                }
              }
            ),
            SizedBox(height:20),
            RaisedButton(
              child: Text('打开外部应用-支付宝'),
              onPressed: () async{
                var url = 'alipays://';
                if (await canLaunch(url)) {
                  await launch(url);
                } else {
                  throw 'Could not launch $url';
                }
              }
            ),
            SizedBox(height:20),
            RaisedButton(
              child: Text('打开QQ聊天界面'),
              onPressed: () async{
                var url = 'mqqwpa://im/chat?chat_type=wpa&uin=2488358015';
                if (await canLaunch(url)) {
                  await launch(url);
                } else {
                  throw 'Could not launch $url';
                }
              }
            ),
          ], 
        ) 
      )
    );
  }
}